package Gr

import (
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
	"math"
)

func (sum SumF) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(sum, parameter, value)
}
func (sum SumF) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}

func (sum SumF) Rows(parameter parDef.Parameter) (result global.Return, err error) {
	var execSql []string
	var event = "[Rows]"
	execSql = append(execSql, fmt.Sprintf("SELECT TABLE_ROWS as `tableRows` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME = '%v'", parameter.Object.Schema, parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	if result.Result, err = stringAnyToInt64(result.Result); err != nil {
		return
	}
	return
}
func (sum SumF) Count(parameter parDef.Parameter) (result global.Return, err error) {
	//var execSql []string
	//var event = "[GrRows]"
	//execSql = append(execSql, fmt.Sprintf("select \n"+
	//	"/*+ SET_VAR(innodb_parallel_read_threads=5) */ count(1) as `tableRows` \n"+
	//	"from %v where 1=1 \n"+
	//	"%v %v %v",
	//	tableObject(parameter.Object.Schema, parameter.Object.Table, parameter.Object.Partition),
	//	whereFilter(parameter.Options.WhereSql),
	//	whereFilter(parameter.Options.WhereAdd),
	//	rowsLimitC(parameter.Options.RowsLimit)))
	//if parameter.ExecSqlFunc != nil {
	//	if result, err = parameter.ExecSqlFunc(modifyGetDbQueryOptions(parameter.GetDbQueryOptions(parameter), dataDispos.String), execSql); err != nil {
	//		err = ref.ErrAddPrintf(event, err)
	//		return
	//	}
	//}
	//if parameter.ExecFunc != nil {
	//	result.Result,err = parameter.ExecFunc(result.Result,nil)
	//}
	return
}
func (sum SumF) Sum(parameter parDef.Parameter) (result global.Return, err error) {
	var sumValue int64
	if result, err = sum.Rows(parameter); err != nil || result.Result == nil {
		return
	}
	sumValue = result.Result.(int64)
	if sumValue > 1000000 {
		return
	}
	if result, err = sum.Count(parameter); err != nil || result.Result == nil {
		return
	}
	return
}
func (sum SumF) DataSizeMB(parameter parDef.Parameter) (result global.Return, err error) {
	var execSql []string
	var event = "[DataSizeMB]"
	execSql = append(execSql, fmt.Sprintf("SELECT ROUND(SUM(DATA_LENGTH) / 1048576, 2) AS `dataSizeMB` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME = '%v'", parameter.Object.Schema, parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	if result.Result, err = stringAnyToInt64(result.Result); err != nil {
		return
	}
	return
}
func (sum SumF) IndexSizeMB(parameter parDef.Parameter) (result global.Return, err error) {
	var execSql []string
	var event = "[IndexSizeMB]"
	execSql = append(execSql, fmt.Sprintf("SELECT ROUND(SUM(INDEX_LENGTH) / 1048576, 2) AS `indexSizeMB` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME = '%v'", parameter.Object.Schema, parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	if result.Result, err = stringAnyToInt64(result.Result); err != nil {
		return
	}
	return
}
func (sum SumF) DataLength(parameter parDef.Parameter) (result global.Return, err error) {
	var dataSize, indexSize float64
	if result, err = sum.DataSizeMB(parameter); err != nil || result.Result == nil {
		return
	}
	dataSize = result.Result.(float64)
	if result, err = sum.IndexSizeMB(parameter); err != nil || result.Result == nil {
		return
	}
	indexSize = result.Result.(float64)
	result.Result = math.Round((dataSize+indexSize)*100) / 100
	return
}

func (sum SumF) AvgRowsSize(parameter parDef.Parameter) (result global.Return, err error) {
	var execSql []string
	var event = "[AvgRowsSize]"
	execSql = append(execSql, fmt.Sprintf("SELECT `AVG_ROW_LENGTH` as `AvgRowLength` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%v' AND TABLE_NAME = '%v'", parameter.Object.Schema, parameter.Object.Table))
	if result, err = forExecQuerySql(event, parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	if result.Result, err = stringAnyToInt64(result.Result); err != nil {
		return
	}
	return
}
